import { InitModalMethod, ParseModalMethod, replaceHtmlFragment } from './defination';

/**
 * 设置表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const initModalData = (form = {}, workstep = {}) => {
	if (!form || !workstep || !workstep.answer) {
		return;
	}
	// yes
	form.nextStepYes = workstep.answer[0]?.do?.id;
	// url
	form.resource = workstep.answer[1]?.do?.data?.data.content;
	//回显原始表单form
	form.wordsSendContents = workstep.form;

	form.name = workstep.name;
	form.wordsSendContents.wordsVariableId = workstep.do.variableId;
	form.wordsSendContents.wordsVariables = workstep.do.variables;
};
InitModalMethod['sendRichText'] = initModalData;
/**
 * 解析表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const parseModalData = (form = {}, workstep = {}) => {
	console.log(form, workstep);
	if (!form || !workstep || !workstep.do) {
		return;
	}
	// yes
	workstep.answer[0].do.id = form.nextStepYes;
	workstep.answer[0].do.no = form.nextStepYes;
	// content
	const { wordage, wordsVariableId = '', wordsVariables = [] } = form.wordsSendContents;
	const data = { content: wordage, attachments: [] };
	workstep.answer[1].do.data.data = data;
	workstep.do.data.data = data;
	workstep.do.variableId = wordsVariableId;
	workstep.do.variables = wordsVariables.filter(item => data.content.indexOf(`{{${item.name}}}`) !== -1);

	//回显原始表单form
	workstep.form = form.wordsSendContents;
	// "fileName": "推送富文本：ddddd",
	workstep.form.fileName = '推送富文本：' + replaceHtmlFragment(wordage);

	workstep.name = form.name;
};
ParseModalMethod['sendRichText'] = parseModalData;
// {
//     "robotProperty": {
//         "needHearing": "preorder",
//         "videoType": "preorder",
//         "audioType": "preorder"
//     },
//     "id": "sendContents_1",
//     "no": "sendContents_1",
//     "type": "sendRichTextNode",
//     "do": {
//         "command": "send-message",
//         "data": {
//             "type": "richtext",
//             "data": {
//                 "content": "<p>ddddd</p>",
//                 "attachments": []
//             }
//         },
//         "variables": [],
//         "variableId": ""
//     },
//     "after": {
//         "command": "receive-message"
//     },
//     "answer": [
//         {
//             "key": [
//                 "ok"
//             ],
//             "do": {
//                 "command": "jump-to",
//                 "id": "BlankNode_2",
//                 "no": "BlankNode_2"
//             }
//         },
//         {
//             "key": "others",
//             "do": {
//                 "command": "send-message",
//                 "data": {
//                     "type": "richtext",
//                     "data": {
//                         "content": "<p>ddddd</p>",
//                         "attachments": []
//                     }
//                 }
//             },
//             "after": {
//                 "command": "receive-message"
//             }
//         }
//     ],
//     "errorRemind": {
//         "maximum": 3
//     },
//     "name": "",
//     "form": {
//         "url": "",
//         "next": "BlankNode",
//         "size": 0,
//         "fileName": "推送富文本：ddddd",
//         "originalname": "",
//         "fileList": [],
//         "uploadUrl": "",
//         "inputUrl": "",
//         "type": "send_richText",
//         "isEdit": true,
//         "wordage": "<p>ddddd</p>",
//         "textData": "ddddd",
//         "wordsVariableId": "",
//         "wordsVariables": [],
//         "pdfList": [],
//         "uploadPdfUrl": ""
//     },
//     "completed": true,
//     "hasStartNode": false
// }
